
# define output folder environment variable
TARGET_NAME=$PROJECT_NAME
BUNDLE_NAME="${PROJECT_NAME}Bundle"
#旧OBJROOT
#OBJROOT_="${OBJROOT}"
#Xcode10 OBJROOT
OBJROOT_="${OBJROOT}/DependentBuilds"
UNIVERSAL_OUTPUTFOLDER=${BUILD_DIR}/${CONFIGURATION}-universal
DEST_DIR=${SRCROOT}/../Libraries/StaticLibs/
BUNDLE_DEST_DIR=${SRCROOT}/../Libraries/Bundles
HEADER_DEST_DIR=${SRCROOT}/../Libraries/Headers/${TARGET_NAME}/
# Step 0. clean
xcodebuild clean -UseModernBuildSystem=NO
# Step 1. Build Device and Simulator versions，xcode10 去掉clean
xcodebuild -configuration ${CONFIGURATION} -target "${TARGET_NAME}" ONLY_ACTIVE_ARCH=NO -sdk iphoneos BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" OBJROOT="${OBJROOT_}" build GCC_PREPROCESSOR_DEFINITIONS=${GCC_PREPROCESSOR_DEFINITIONS}
#如果想使用旧的编译器与clean，请在上编译语句中加入下面的语句，显式指定编译器
#-UseModernBuildSystem=NO
xcodebuild -configuration ${CONFIGURATION} -target "${TARGET_NAME}" -arch x86_64 -sdk iphonesimulator BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" OBJROOT="${OBJROOT_}" build GCC_PREPROCESSOR_DEFINITIONS=${GCC_PREPROCESSOR_DEFINITIONS}

# make sure the output directory exists
mkdir -p "${UNIVERSAL_OUTPUTFOLDER}"

# Step 2. Create universal binary file using lipo ，xcode10 -output需放后面
lipo -create "${BUILD_DIR}/${CONFIGURATION}-iphoneos/lib${TARGET_NAME}.a" "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/lib${TARGET_NAME}.a" -output "${UNIVERSAL_OUTPUTFOLDER}/${TARGET_NAME}.a"

# Last touch. copy the header files. Just for convenience
if [ "$1" = "copyHeader" ]
then
cp -R "${BUILD_DIR}/${CONFIGURATION}-iphoneos/include/${TARGET_NAME}" "${UNIVERSAL_OUTPUTFOLDER}/"
fi

# Step 3. Build and copy Bundle，xcode10 去掉clean
rm -rf "${UNIVERSAL_OUTPUTFOLDER}/${BUNDLE_NAME}.bundle"
xcodebuild -configuration ${CONFIGURATION} -target "${BUNDLE_NAME}" ONLY_ACTIVE_ARCH=NO -sdk iphoneos BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" OBJROOT="${OBJROOT_}" build
cp -R "${BUILD_DIR}/${CONFIGURATION}-iphoneos/${BUNDLE_NAME}.bundle" "${UNIVERSAL_OUTPUTFOLDER}/"
#open "${UNIVERSAL_OUTPUTFOLDER}/"

#copy .a & bundle
rm -rf "${BUNDLE_DEST_DIR}/${BUNDLE_NAME}.bundle"
rm -rf "${DEST_DIR}/${TARGET_NAME}.a"
echo "${DEST_DIR}"
cp -R "${UNIVERSAL_OUTPUTFOLDER}/${BUNDLE_NAME}.bundle" "${BUNDLE_DEST_DIR}/"
cp -R "${UNIVERSAL_OUTPUTFOLDER}/${TARGET_NAME}.a" "${DEST_DIR}/"
if [ "$1" = "copyHeader" ]
then
cp -R "${UNIVERSAL_OUTPUTFOLDER}/${TARGET_NAME}/" "${HEADER_DEST_DIR}" || exit 1
fi
